<?php

/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | registration
// +---------------------------------------------------------------------------+
// $Id: registration.php
// public_html/userbox/registration.php
// 20101208 tsuchitani AT ivywe DOT co DOT jp
// 20110905

define ('THIS_SCRIPT', 'userbox/registration.php');
//define ('THIS_SCRIPT', 'userbox/test.php');


require_once('../lib-common.php');
if (!in_array('userbox', $_PLUGINS)) {
    echo COM_refresh($_CONF['site_url'] . '/index.php');
    exit;
}

require_once ($_CONF['path'] . 'plugins/userbox/lib/lib_datetimeedit.php');
require_once $_CONF['path_system'] . 'lib-user.php';

//$edt_flg=FALSE;


// ログアウトしてたほうがよい？
// 権限チェック ???
//if  ($_USER['uid']>0 ) {
//    exit;
//}

    if ($_CONF['disable_new_user_registration']) {
        $display .= COM_siteHeader ('menu', $LANG04[22]);
        $display .= COM_startBlock ($LANG04[22], '',
                            COM_getBlockTemplate ('_msg_block', 'header'))
                 . $LANG04[122]
                 . COM_endBlock (COM_getBlockTemplate ('_msg_block', 'footer'));
        $display .= COM_siteFooter ();
    }






// +---------------------------------------------------------------------------+
// | 機能  編集画面表示                                                        |
// | 書式 fncEdit($edt_flg,$msg,$errmsg)                                 |
// +---------------------------------------------------------------------------+
// | 引数 $edt_flg:                                                            |
// | 引数 $msg:メッセージ番号                                                  |
// +---------------------------------------------------------------------------+
// | 戻値 nomal:編集画面                                                       |
// +---------------------------------------------------------------------------+
// 20101208
function fncEdit(
    $msg=""
    ,$errmsg=""
)
{

    $pi_name="userbox";

    global $_CONF;
    global $_TABLES;
    global $LANG_ADMIN;
    global $MESSAGE;
    global $LANG_ACCESS;
    global $_USER;
    global $LANG28;
    global $LANG04;

    global $_USERBOX_CONF;
    global $LANG_USERBOX_ADMIN;

    $retval = '';

    $delflg=false;

    $addition_def=DATABOX_getadditiondef($pi_name);

    //メッセージ表示
    if (!empty ($msg)) {
        $retval .= COM_startBlock ($LANG_USERBOX_ADMIN['err'], '',
                           COM_getBlockTemplate ('_msg_block', 'header'))
                . $MESSAGE[$msg]
                . $errmsg
                . COM_endBlock (COM_getBlockTemplate ('_msg_block', 'footer'));
        // clean 'em up
        $code=COM_applyFilter($_POST['code']);
        $title = COM_applyFilter($_POST['title']);
        $username=COM_applyFilter($_POST['username']);//@@@@@

        $email=COM_applyFilter($_POST['email']);//@@@@@
        $email_conf=COM_applyFilter($_POST['email_conf']);//@@@@@

        $fullname = COM_applyFilter($_POST['fullname']);//@@@@@

        $page_title = COM_applyFilter($_POST['page_title']);
        $description=$_POST['description'];//COM_applyFilter($_POST['description']);
        $defaulttemplatesdirectory = COM_applyFilter($_POST['defaulttemplatesdirectory']);//@@@@@@

        $draft_flag = COM_applyFilter ($_POST['draft_flag'],true);
        $hits = COM_applyFilter ($_POST['hits'],true);
        $comments = COM_applyFilter ($_POST['comments'],true);
        $commentcode = COM_applyFilter ($_POST['commentcode'],true);

        //@@@@@
        $comment_expire_flag = COM_applyFilter ($_POST['comment_expire_flag'],true);
        if ($comment_expire_flag===0){
            $w = mktime(0, 0, 0, date('m'),
               date('d') + $_CONF['article_comment_close_days'], date('Y'));
               $comment_expire_year=date('Y', $w);
            $comment_expire_month=date('m', $w);
            $comment_expire_day=date('d', $w);
            $comment_expire_hour=0;
            $comment_expire_minute=0;
        }else{
            $comment_expire_month = COM_applyFilter ($_POST['comment_expire_month'],true);
            $comment_expire_day = COM_applyFilter ($_POST['comment_expire_day'],true);
            $comment_expire_year = COM_applyFilter ($_POST['comment_expire_year'],true);
            $comment_expire_hour = COM_applyFilter ($_POST['comment_expire_hour'],true);
            $comment_expire_minute = COM_applyFilter ($_POST['comment_expire_minute'],true);
        }

        $meta_description = COM_applyFilter ($_POST['meta_description']);
        $meta_keywords = COM_applyFilter ($_POST['meta_keywords']);

        $category = $_POST['category'];

        $additionfields=$_POST['afield'];
        $additionfields=DATABOX_cleanaddtiondatas($additionfields,$addition_def);

        $owner_id = COM_applyFilter ($_POST['owner_id'],true);
        $group_id = COM_applyFilter ($_POST['group_id'],true);

        //
        $array['perm_owner']=$_POST['perm_owner'];
        $array['perm_group']=$_POST['perm_group'];
        $array['perm_members']=$_POST['perm_members'];
        $array['perm_anon']=$_POST['perm_anon'];

        if (is_array($array['perm_owner']) || is_array($array['perm_group']) ||
                is_array($array['perm_members']) ||
                is_array($array['perm_anon'])) {

            list($perm_owner, $perm_group, $perm_members, $perm_anon)
                = SEC_getPermissionValues($array['perm_owner'], $array['perm_group'], $array['perm_members'], $array['perm_anon']);

        } else {
            $perm_owner   = $array['perm_owner'];
            $perm_group   = $array['perm_group'];
            $perm_members = $array['perm_members'];
            $perm_anon    = $array['perm_anon'];
        }


        //編集日
        $modified_autoupdate = COM_applyFilter ($_POST['modified_autoupdate'],true);
        $modified_month = COM_applyFilter ($_POST['modified_month'],true);
        $modified_day = COM_applyFilter ($_POST['modified_day'],true);
        $modified_year = COM_applyFilter ($_POST['modified_year'],true);
        $modified_hour = COM_applyFilter ($_POST['modified_hour'],true);
        $modified_minute = COM_applyFilter ($_POST['modified_minute'],true);
        //公開日
        $released_month = COM_applyFilter ($_POST['released_month'],true);
        $released_day = COM_applyFilter ($_POST['released_day'],true);
        $released_year = COM_applyFilter ($_POST['released_year'],true);
        $released_hour = COM_applyFilter ($_POST['released_hour'],true);
        $released_minute = COM_applyFilter ($_POST['released_minute'],true);
        //公開終了日
        $expired_available = COM_applyFilter ($_POST['expired_available'],true);
        $expired_flag = COM_applyFilter ($_POST['expired_flag'],true);

        if ($expired_flag===0){
            $w = mktime(0, 0, 0, date('m'),
                date('d') + $_CONF['article_comment_close_days'], date('Y'));
            $expired_year=date('Y', $w);
            $expired_month=date('m', $w);
            $expired_day=date('d', $w);
            $expired_hour=0;
            $expired_minute=0;
        }else{
            $expired_month = COM_applyFilter ($_POST['expired_month'],true);
            $expired_day = COM_applyFilter ($_POST['expired_day'],true);
            $expired_year = COM_applyFilter ($_POST['expired_year'],true);
            $expired_hour = COM_applyFilter ($_POST['expired_hour'],true);
            $expired_minute = COM_applyFilter ($_POST['expired_minute'],true);
        }
        //作成日付
        $created_month=COM_applyFilter ($_POST['created_month'],true);
        $created_day = COM_applyFilter ($_POST['created_day'],true);
        $created_year =COM_applyFilter ($_POST['created_year'],true);
        $created_hour = COM_applyFilter ($_POST['created_hour'],true);
        $created_minute = COM_applyFilter ($_POST['created_minute'],true);
        $created = COM_applyFilter ($_POST['created']);

        $orderno = COM_applyFilter ($_POST['orderno']);

        $uuid=$_USER['uid'];
        $udatetime=COM_applyFilter ($_POST['udatetime']);//"";


    }else{


        $code ="";
        $title ="";
        $username="";//@@@@@
        $email="";//@@@@@
        $email_conf="";//@@@@@
        $fullname="";//@@@@@

        $description="";
        $defaulttemplatesdirectory=null;

        $hits =0;
        $comments=0;

        $comment_expire_flag = 0;
        $w = mktime(0, 0, 0, date('m'),
             date('d') + $_CONF['article_comment_close_days'], date('Y'));
        $comment_expire_year=date('Y', $w);
        $comment_expire_month=date('m', $w);
        $comment_expire_day=date('d', $w);
        $comment_expire_hour=0;
        $comment_expire_minute=0;

        $commentcode =0;

        $meta_description ="";
        $meta_keywords ="";

        $category = "";
        $additionfields=array();
        $additionfields = DATABOX_getadditiondatas(0,$pi_name);

        //
        $owner_id =0;
        $group_id =SEC_getFeatureGroup('userbox.admin', $_USER['uid']);//??????

        $array = array();
        SEC_setDefaultPermissions($array, $_USERBOX_CONF['default_perm']);
        $perm_owner = $array['perm_owner'];
        $perm_group = $array['perm_group'];
        $perm_anon = $array['perm_anon'];
        $perm_members = $array['perm_members'];

        //
        $draft_flag=$_USERBOX_CONF['user_draft_default'];



        //編集日付
        $modified_month = date('m');
        $modified_day = date('d');
        $modified_year = date('Y');
        $modified_hour = date('H');
        $modified_minute = date('i');
        //作成日付
        $created=0;
        $created_month=0;
        $created_day = 0;
        $created_year = 0;
        $created_hour = 0;
        $created_minute = 0;
        //公開日
        $released_month=$modified_month;
        $released_day = $modified_day;
        $released_year = $modified_year;
        $released_hour = $modified_hour;
        $released_minute = $modified_minute;
        //公開終了日
        $expired_flag=0;
        $w = mktime(0, 0, 0, date('m'),
             date('d') + $_CONF['article_comment_close_days'], date('Y'));
        $expired_year=date('Y', $w);
        $expired_month=date('m', $w);
        $expired_day=date('d', $w);
        $expired_hour=0;
        $expired_minute=0;

        $orderno ="";

        $uuid=0;
        $udatetime="";//"";

    }


    //-----
    $retval .= COM_startBlock($LANG04[22]);

    //template フォルダ
    $tmplfld=DATABOX_templatePath('registration','default',$pi_name);
    $templates = new Template($tmplfld);

    $templates->set_file (array (
                'editor' => 'registration_editor.thtml',
                'row'   => 'row.thtml',
                'col'   => "registration_col_detail.thtml",
                'date_row'   => 'datepicker_row.thtml',
                'date_col'   => "datepicker_col.thtml",


            ));

    //--
    if (($_CONF['meta_tags'] > 0) && ($_USERBOX_CONF['meta_tags'] > 0)) {
        $templates->set_var('hide_meta', '');
    } else {
        $templates->set_var('hide_meta', ' style="display:none;"');
    }

    $templates->set_var('lang_register', $LANG04[27]);
    $templates->set_var('lang_instructions', $LANG04[23]);

//    PLG_templateSetVars ('registration', $user_templates);




    $templates->set_var('about_thispage', $LANG_USERBOX_ADMIN['about_admin_data']);
    $templates->set_var('lang_must', $LANG_USERBOX_ADMIN['must']);
    $templates->set_var('site_url', $_CONF['site_url']);
    $templates->set_var('site_admin_url', $_CONF['site_admin_url']);

    $token = SEC_createToken();
    $retval .= SEC_getTokenExpiryNotice($token);
    $templates->set_var('gltoken_name', CSRF_TOKEN);
    $templates->set_var('gltoken', $token);
    $templates->set_var ( 'xhtml', XHTML );

    $templates->set_var('script', THIS_SCRIPT);

    $templates->set_var('dateformat', $_USERBOX_CONF['dateformat']);

    //
    $templates->set_var('lang_link_admin', $LANG_USERBOX_ADMIN['link_admin']);
    $templates->set_var('lang_link_admin_top', $LANG_USERBOX_ADMIN['link_admin_top']);
    $templates->set_var('lang_link_public', $LANG_USERBOX_ADMIN['link_public']);
    $templates->set_var('lang_link_list', $LANG_USERBOX_ADMIN['link_list']);
    $templates->set_var('lang_link_detail', $LANG_USERBOX_ADMIN['link_detail']);

    //id
    $templates->set_var('lang_id', $LANG_USERBOX_ADMIN['id']);
    //@@@@@ $templates->set_var('help_id', $LANG_USERBOX_ADMIN['help']);
    $templates->set_var('id', $id);



//$LANG28 = array(
//    2 => 'ユーザID',
    $templates->set_var('lang_uid', $LANG28['2']);

//    3 => 'ユーザ名', username

    //$templates->set_var('lang_username', $LANG28['3']);
    $templates->set_var('lang_username', $LANG04[2]);
    $templates->set_var ('username', $username);

    $templates->set_var('lang_email', $LANG04[5]);
    $templates->set_var ('email', $email);

    $templates->set_var('lang_email_conf', $LANG04[124]);
    $templates->set_var ('email_conf', $email_conf);

    $templates->set_var('lang_warning', $LANG04[24]);
    $templates->set_var('lang_register', $LANG04[27]);


//    4 => '氏名', fullname
    $templates->set_var('lang_fullname', $LANG28['4']);
    $templates->set_var ('fullname', $fullname);


    //下書
    $templates->set_var('lang_draft', $LANG_USERBOX_ADMIN['draft']);
    if  ($draft_flag==1) {
        $templates->set_var('draft_flag', "checked=checked");
    }else{
        $templates->set_var('draft_flag', "");
    }

    //
    $templates->set_var('lang_field', $LANG_USERBOX_ADMIN['field']);
    $templates->set_var('lang_fields', $LANG_USERBOX_ADMIN['fields']);
    $templates->set_var('lang_content', $LANG_USERBOX_ADMIN['content']);
    $templates->set_var('lang_templatesetvar', $LANG_USERBOX_ADMIN['templatesetvar']);

    //基本項目
    $templates->set_var('lang_basicfields', $LANG_USERBOX_ADMIN['basicfields']);
    //コード＆タイトル＆説明＆テンプレートセット値
    $templates->set_var('lang_code', $LANG_USERBOX_ADMIN['code']);
    if ($_USERBOX_CONF['datacode']){
        $templates->set_var('lang_must_code', $LANG_USERBOX_ADMIN['must']);
    }else{
        $templates->set_var('lang_must_code', "");
    }
    $templates->set_var ('code', $code);
    $templates->set_var('lang_title', $LANG_USERBOX_ADMIN['title']);
    $templates->set_var ('title', $title);
    $templates->set_var('lang_page_title', $LANG_USERBOX_ADMIN['page_title']);
    $templates->set_var ('page_title', $page_title);
    $templates->set_var('lang_description', $LANG_USERBOX_ADMIN['description']);
    $templates->set_var ('description', $description);
    $templates->set_var('lang_defaulttemplatesdirectory', $LANG_USERBOX_ADMIN['defaulttemplatesdirectory']);
    $templates->set_var ('defaulttemplatesdirectory', $defaulttemplatesdirectory);
    //meta_description
    $templates->set_var('lang_meta_description', $LANG_USERBOX_ADMIN['meta_description']);
    $templates->set_var ('meta_description', $meta_description);

    //meta_keywords
    $templates->set_var('lang_meta_keywords', $LANG_USERBOX_ADMIN['meta_keywords']);
    $templates->set_var ('meta_keywords', $meta_keywords);

    //hits
    $templates->set_var('lang_hits', $LANG_USERBOX_ADMIN['hits']);
    $templates->set_var ('hits', $hits);

    //comments
    $templates->set_var('lang_comments', $LANG_USERBOX_ADMIN['comments']);
    $templates->set_var ('comments', $comments);

    //commentcode
    $templates->set_var('lang_commentcode', $LANG_USERBOX_ADMIN['commentcode']);
    $templates->set_var ('commentcode', $commentcode);
    $optionlist_commentcode=COM_optionList ($_TABLES['commentcodes'], 'code,name',$commentcode);
    $templates->set_var ('optionlist_commentcode', $optionlist_commentcode);

    //comment_expire
    $templates->set_var('lang_enabled', $LANG_USERBOX_ADMIN['enabled']);

    if ($comment_expire_flag===0){
        $templates->set_var('show_comment_expire', 'false');
        $templates->set_var('is_checked_comment_expire', '');

    }else{
        $templates->set_var('show_comment_expire', 'true');
        $templates->set_var('is_checked_comment_expire', 'checked="checked"');
    }

    $templates->set_var('lang_comment_expire', $LANG_USERBOX_ADMIN['comment_expire']);
    $w=COM_convertDate2Timestamp(
        $comment_expire_year."-".$comment_expire_month."-".$comment_expire_day
        , $comment_expire_hour.":".$comment_expire_minute."::00"
        );
    $datetime_comment_expire=LIB_datetimeedit($w,"LANG_USERBOX_ADMIN","comment_expire");
    $templates->set_var('datetime_comment_expire', $datetime_comment_expire);

    //編集日
    $templates->set_var ('lang_modified_autoupdate', $LANG_USERBOX_ADMIN['modified_autoupdate']);
    $templates->set_var ('lang_modified', $LANG_USERBOX_ADMIN['modified']);
    $w=COM_convertDate2Timestamp(
        $modified_year."-".$modified_month."-".$modified_day
        , $modified_hour.":".$modified_minute."::00"
        );
    $datetime_modified=LIB_datetimeedit($w,"LANG_DATABOX_ADMIN","modified");
    $templates->set_var ('datetime_modified', $datetime_modified);
    //公開日
    $templates->set_var ('lang_released', $LANG_USERBOX_ADMIN['released']);
    $w=COM_convertDate2Timestamp(
        $released_year."-".$released_month."-".$released_day
        , $released_hour.":".$released_minute."::00"
        );
    $datetime_released=LIB_datetimeedit($w,"LANG_DATABOX_ADMIN","released");
    $templates->set_var ('datetime_released', $datetime_released);
    //公開終了日
    $templates->set_var ('lang_expired', $LANG_USERBOX_ADMIN['expired']);
    //if ($expired=="0000-00-00 00:00:00"){
    if ($expired_flag==0){
        $templates->set_var('show_expired', 'false');
        $templates->set_var('is_checked_expired', '');

    }else{
        $templates->set_var('show_expired', 'true');
        $templates->set_var('is_checked_expired', 'checked="expired"');
    }
    $templates->set_var('lang_expired', $LANG_USERBOX_ADMIN['expired']);
    $w=COM_convertDate2Timestamp(
        $expired_year."-".$expired_month."-".$expired_day
        , $expired_hour.":".$expired_minute."::00"
        );
    $datetime_expired=LIB_datetimeedit($w,"LANG_DATABOX_ADMIN","expired");
    $templates->set_var('datetime_expired', $datetime_expired);

    //カテゴリ
    $templates->set_var('lang_category', $LANG_USERBOX_ADMIN['category']);
    $checklist_category=DATABOX_getcheckList ("category",$category,$pi_name);
    $templates->set_var('checklist_category', $checklist_category);

    //追加項目
    $chk_user=1011;
    $templates->set_var('lang_additionfields', $LANG_USERBOX_ADMIN['additionfields']);
    $rt=DATABOX_getaddtionfieldsEdit($additionfields,$addition_def,$templates,$chk_user,$pi_name);
    $rt=DATABOX_getaddtionfieldsJS($additionfields,$addition_def,$templates,$chk_user,$pi_name);

    //保存日時
    $templates->set_var ('lang_udatetime', $LANG_USERBOX_ADMIN['udatetime']);
    $templates->set_var ('udatetime', $udatetime);
    $templates->set_var ('lang_uuid', $LANG_USERBOX_ADMIN['uuid']);
    $templates->set_var ('uuid', $uuid);
    //作成日付
    $templates->set_var ('lang_created', $LANG_USERBOX_ADMIN['created']);
    $templates->set_var ('created', $created);

    //アクセス権
    $templates->set_var('lang_accessrights',$LANG_ACCESS['accessrights']);
    $templates->set_var('lang_owner', $LANG_ACCESS['owner']);

    $owner_name = COM_getDisplayName($owner_id);
    $templates->set_var('owner_name', $owner_name);
    $templates->set_var('owner_id', $owner_id);
    $templates->set_var('lang_group', $LANG_ACCESS['group']);
    $templates->set_var('group_dropdown',SEC_getGroupDropdown ($group_id, 3));
    $templates->set_var('lang_permissions', $LANG_ACCESS['permissions']);
    $templates->set_var('lang_perm_key', $LANG_ACCESS['permissionskey']);
    $templates->set_var('permissions_editor'
                , SEC_getPermissionsHTML(
                         $perm_owner
                        ,$perm_group
                        ,$perm_members
                        ,$perm_anon));

    $templates->set_var('permissions_msg', $LANG_ACCESS['permmsg']);
    $templates->set_var('lang_permissions_msg', $LANG_ACCESS['permmsg']);

    //
    $templates->parse('output', 'editor');
    $retval .= $templates->finish($templates->get_var('output'));
    $retval .= COM_endBlock ();

    return $retval;
}


// +---------------------------------------------------------------------------+
// | 機能  保存                                                                |
// | 書式 fncCreate()
// +---------------------------------------------------------------------------+
// | 戻値 nomal:topページに戻る＆メッセージ
// +---------------------------------------------------------------------------+
function fncCreate ()
{

    $pi_name="userbox";

    global $_CONF;
    global $_TABLES;
    global $_USER;

    global $_USERBOX_CONF;
    global $LANG_USERBOX_ADMIN;

    $addition_def=DATABOX_getadditiondef($pi_name);

    $retval = '';




    // clean 'em up

    $email = COM_applyFilter ($_POST['email']);
    $email_conf = COM_applyFilter ($_POST['email_conf']);

    //

    $username = COM_applyFilter($_POST['username']);
    $username = addslashes (COM_checkHTML (COM_checkWords ($username)));

    $fullname = COM_applyFilter($_POST['fullname']);
    $fullname = addslashes (COM_checkHTML (COM_checkWords ($fullname)));

    $page_title = COM_applyFilter($_POST['page_title']);
    $page_title = addslashes (COM_checkHTML (COM_checkWords ($page_title)));

    $description=$_POST['description'];//COM_applyFilter($_POST['description']);
    $description=addslashes (COM_checkHTML (COM_checkWords ($description)));

    $category = $_POST['category'];

    //@@@@@
    $additionfields=$_POST['afield'];
    $additionfields=DATABOX_cleanaddtiondatas($additionfields,$addition_def);

//            $hits =0;
//            $comments=0;

    //-----
    $type=1;
    $uuid=$_USER['uid'];


    // CHECK　はじめ
    $err="";

    $err.= fncCreateCheck($username, $email, $email_conf);

    //----追加項目チェック
    $err.=databox_checkddtiondatas($additionfields,$addition_def);


    //errorのあるとき
    if ($err<>"") {
        $page_title=$LANG_USERBOX_ADMIN['piname'].$LANG_USERBOX_ADMIN['edit'];
        $retval .= DATABOX_siteHeader($pi_name,'_admin',$page_title);

        $retval .= fncEdit(3,$err);
        $retval .= DATABOX_siteFooter($pi_name,'_admin');

        return $retval;

    }
    // CHECK　おわり

    //-----
    // 新規登録時

    $uid = USER_createAccount ($username, $email);
    $id = $uid;
    $owner_id =$uid;
    if ($_USER['uid']<>""){
        $uuid =$_USER['uid'];
    }else{
        $uuid =$uid;
    }

    $status=DB_getItem ($_TABLES['users'], 'status',"uid = '{$uid}'");

    $fields=LB."id";
    $values=LB."$id";


    $created=date("Y-m-d H:i:s");
    $modified=$created;
    $released=$created;
    $commentcode =0;
    $comment_expire='0000-00-00 00:00:00';
    $expired='0000-00-00 00:00:00';
    //

    $defaulttemplatesdirectory=null;

    //---
    $meta_description = "";
    $meta_keywords = "";
    $group_id =SEC_getFeatureGroup('userbox.admin', $_USER['uid']);

    $array = array();

    SEC_setDefaultPermissions($array, $_USERBOX_CONF['default_perm']);


    $perm_owner = $array['perm_owner'];
    $perm_group = $array['perm_group'];
    $perm_anon = $array['perm_anon'];
    $perm_members = $array['perm_members'];

    $draft_flag=$_USERBOX_CONF['user_draft_default'];
    //管理者の承認が必要(usersubmission)
    if (($_CONF['usersubmission'] == 1)
            && ($status == USER_ACCOUNT_AWAITING_APPROVAL)){
        $draft_flag=1;

    }

    //-----

    $fields.=",defaulttemplatesdirectory";//
    $values.=",'$defaulttemplatesdirectory'";


    $fields.=",draft_flag";
    $values.=",$draft_flag";

    $fields.=",meta_description";//
    $values.=",'$meta_description'";

    $fields.=",meta_keywords";//
    $values.=",'$meta_keywords'";

    $fields.=",commentcode";//
    $values.=",$commentcode";

    $fields.=",comment_expire";//
    $values.=",'$comment_expire'";

    $fields.=",language_id";//
    $values.=",'$language_id'";

    $fields.=",owner_id";
    $values.=",$owner_id";

    $fields.=",group_id";
    $values.=",$group_id";

    $fields.=",perm_owner";
    $values.=",$perm_owner";

    $fields.=",perm_group";
    $values.=",$perm_group";

    $fields.=",perm_members";
    $values.=",$perm_members";

    $fields.=",perm_anon";
    $values.=",$perm_anon";

    $fields.=",modified";
    $values.=",'$modified'";

    $fields.=",created";
    $values.=",'$created'";

    $fields.=",expired";
    $values.=",'$expired'";

    $fields.=",released";
    $values.=",'$released'";

    $hits=0;
    $comments=0;

    $fields.=",page_title";//
    $values.=",'$page_title'";

    $fields.=",description";//
    $values.=",'$description'";


    $fields.=",hits";//
    $values.=",$hits";

    $fields.=",comments";//
    $values.=",$comments";

    $fields.=",uuid";
    $values.=",$uuid";

    $fields.=",udatetime".LB;
    $values.=",NOW( )".LB;

    DB_save($_TABLES['USERBOX_base'],$fields,$values);

    //カテゴリ
    $rt=DATABOX_savecategorydatas($id,$category,$pi_name);

    //追加項目@@@@@
    $rt=DATABOX_saveaddtiondatas($id,$additionfields,$addition_def,$pi_name);

    //user (コアのテーブル)
    $sql="UPDATE ".$_TABLES['users'] ." SET ";

    $sql.=" fullname ='".$fullname."'";

    $sql.=" WHERE uid=".$id ;
    DB_query($sql);

    //-----------

    //管理者の承認が必要(usersubmission)
    if (($_CONF['usersubmission'] == 1)
            && ($status == USER_ACCOUNT_AWAITING_APPROVAL)){
        $msgno=48;
        $rt=fncsendmail ($id);
    }else{
        $mailresult = USER_createAndSendPassword ($username, $email, $uid);

        if ($mailresult == false) {
            //    85 => 'メール送信エラーです。',
            $msgno=85;
        } else {
            //    1 => "パスワードをメールで送信しましたので～
            $msgno=1;
            $rt=fncsendmail ($id);
        }
    }

    //exit;// debug 用


    $return_page = "{$_CONF['site_url']}/index.php?msg={$msgno}";


    return COM_refresh ($return_page);

}

// +---------------------------------------------------------------------------+
// | 機能  メール送信                                                          |
// | 書式 fncsendmail ()                                                       |
// +---------------------------------------------------------------------------+
// | 戻値 nomal:                                                               |
// +---------------------------------------------------------------------------+
function fncsendmail (
    $id
    )
{

    $pi_name="userbox";

    global $_CONF;
    global $_TABLES;
    global $LANG_USERBOX_MAIL;
    global $LANG_USERBOX_ADMIN;
    global $_USERBOX_CONF ;
    global $LANG28;
    global $_USER;

    $retval = '';

    $site_name=$_CONF['site_name'];

    if ($_USER['uid']<>""){
        $subject=$LANG_USERBOX_MAIL['subject_regist1'];
        $message=$LANG_USERBOX_MAIL['message_regist1'];

    }else{
        $subject=$LANG_USERBOX_MAIL['subject_regist2'];
        $message=$LANG_USERBOX_MAIL['message_regist2'];

    }


    $sql = "SELECT ";

    $sql .= " t1.*";
    $sql .= " ,t2.uid";
    $sql .= " ,t2.username";
    $sql .= " ,t2.fullname";
    $sql .= " ,t2.email";

    $sql .= " FROM ";
    $sql .= $_TABLES['USERBOX_base']." AS t1";
    $sql .= ",".$_TABLES['users']." AS t2";

    $sql .= " WHERE ";
    $sql .= " t1.id = $id";
    $sql .= " AND t1.id = t2.uid";

    $result = DB_query ($sql);
    $numrows = DB_numRows ($result);

    if ($numrows > 0) {

        $A = DB_fetchArray ($result);
        $email=$A['email'];

            //下書
        if ($A['draft_flag']==1) {
            $msg.=$LANG_USERBOX_ADMIN['draft'].LB;
        }

        //コア
        $msg.=$LANG28['2'].":".$A['uid'].LB;
        $msg.=$LANG28['3'].":".$A['username'].LB;
        $msg.=$LANG28['4'].":".$A['fullname'].LB;

        //基本項目
        $msg.= $LANG_USERBOX_ADMIN['page_title'].":".$A['page_title'].LB;
        $msg.= $LANG_USERBOX_ADMIN['description'].":".$A['description'].LB;

        //カテゴリ
        $msg.=DATABOX_getcategoriesText($id ,0,$pi_name);

        //追加項目
        $group_id = stripslashes($A['group_id']);
        $owner_id = stripslashes($A['owner_id']);
        $chk_user=DATABOX_chkuser($group_id,$owner_id,"userbox.admin");
        $addition_def=DATABOX_getadditiondef($pi_name);
        $additionfields = DATABOX_getadditiondatas($id,$pi_name);
        $msg.=DATABOX_getaddtionfieldsText($additionfields,$addition_def,$chk_user,$pi_name);

        //タイムスタンプ　更新ユーザ
        $msg.= $LANG_USERBOX_ADMIN['udatetime'].":".$A['udatetime'].LB;
        $msg.= $LANG_USERBOX_ADMIN['uuid'].":".$A['uuid'].LB;

        //URL
        $url=$_CONF['site_url'] . "/userbox/profile.php";
        $url.="?";
        if ($_USERBOX_CONF['datacode']){
            $url.="m=code";
            $url.="&code=".$A['username'];
        }else{
            $url.="m=id";
            $url.="&id=".$A['id'];
        }
        $url = COM_buildUrl( $url );
    }


    $message.=$msg.LB;
    $message.=$url.LB;
    $message.=$LANG_USERBOX_MAIL['sig'].LB;

    $mail_to=$_USERBOX_CONF['mail_to'];
    //--- to user
    COM_mail ($email, $subject, $message);
    //--- to admin
    if ($mail_to<>""){
        $to=implode($mail_to,",");
        COM_mail ($to, $subject, $message);
    }
    return $retval;
}







/////
// +---------------------------------------------------------------------------+
// | 機能  ユーザ名とメールアドレスのチェック
// | 書式 fncCreateCheck($username,$email,$email_conf)
// +---------------------------------------------------------------------------+
// | 戻値 nomal:topページに戻る＆メッセージ
// +---------------------------------------------------------------------------+
function fncCreateCheck (
    $username
    ,$email
    ,$email_conf
)
{
    global $_CONF;
    global $_TABLES;
    global $LANG01;
    global $LANG04;

    $msg = '';

    $username = trim ($username);
    $email = trim ($email);
    $email_conf = trim ($email_conf);

    //自動的に拒否するドメイン(disallow_domains)
    if (!isset ($_CONF['disallow_domains'])) {
        $_CONF['disallow_domains'] = '';
    }

    if (COM_isEmail ($email) && !empty ($username) && ($email === $email_conf)
            && !USER_emailMatches ($email, $_CONF['disallow_domains'])
            && (strlen ($username) <= 16)) {

        $ucount = DB_count ($_TABLES['users'], 'username',
                            addslashes ($username));
        $ecount = DB_count ($_TABLES['users'], 'email', addslashes ($email));

        if ($ucount == 0 AND $ecount == 0) {
            // Let plugins have a chance to decide what to do before creating the user, return errors.
            $msg .= PLG_itemPreSave ('registration', $username);
        } else {
            //19 => 'ユーザ名かメールアドレスがすでに使われています。',
            $msg .= $LANG04[19];
        }
    } else if ($email !== $email_conf) {
        $msg .= $LANG04[125];

    } else { // invalid username or email address
        if ((empty ($username)) || (strlen($username) > 16)) {
            $msg .= $LANG01[32]; // invalid username
        } else {
            $msg .= $LANG04[18]; // invalid email address
        }

    }

    return $msg;
}

// +---------------------------------------------------------------------------+
// | MAIN                                                                      |
// +---------------------------------------------------------------------------+
//############################
$pi_name    = 'userbox';
//############################


// 引数
if (isset ($_REQUEST['mode'])) {
    $mode = COM_applyFilter ($_REQUEST['mode'], false);
}
//

$display="";

//echo "mode=".$mode."<br>";
switch ($mode) {

    case 'create':
        $display .= fncCreate();
        break;

    default:// 初期表示、一覧表示
        $page_title=$LANG_USERBOX_ADMIN['piname'].$LANG_USERBOX_ADMIN['edit'];
        $display .= DATABOX_siteHeader($pi_name,'_admin',$page_title);
        $display .= fncEdit();
        $display .= DATABOX_siteFooter($pi_name,'_admin');


}



echo $display;

?>
